package com.google.code.hbt.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.google.code.hbt.entity.Match;

public enum MatchDao {
	INSTANCE;

	public List<Match> listMatches() {
		EntityManager em = EMFService.get().createEntityManager();
		// Read the existing entries
		Query q = em.createQuery("select m from Match m");

		@SuppressWarnings("unchecked")
		List<Match> todos = q.getResultList();
		return todos;
	}

	public void add(String userId, String summery, String description,
			String url) {
		synchronized (this) {
			EntityManager em = EMFService.get().createEntityManager();
			Match match = new Match();
			match.setSummary("MySummary");
			em.persist(match);
			em.close();
		}
	}

	public void remove(long id) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			Match match = em.find(Match.class, id);
			em.remove(match);
		} finally {
			em.close();
		}
	}
}